<template>
<div class="page">
<div class="i-all">
   <img  class="iimg" :src='strategy.ssrc' @click="previewImg(strategy)"/>
   <div class="isbiao">{{strategy.sbiao}}
    
   </div>
  
   <div v-if="isCollected==false" @click="strategylog" >
     <!-- <div v-if="isCollected=false"></div> -->
     <img class="anniu" @click="handleCollection" src="https://my-wechatl.oss-cn-beijing.aliyuncs.com/dianzan_1.png" alt="">
   </div>
   <div v-if="isCollected!=false" @click="strategylog_no" >
     <!-- <div v-if="isCollected=false"></div> -->
     <img class="anniu" @click="handleCollection" src="https://my-wechatl.oss-cn-beijing.aliyuncs.com/dianzan.png" alt="">
</div>
  </div>
</div>
</template>


<script>
import {mapState} from 'vuex'
import {post,showModel} from '@/utli'
export default {
     props: ['strategy',
              ],
       data(){
            return{
               isCollected:false,
               detailObj: {},
            }
        },
        beforeMount(){
        //   console.log(this)
        //   this.index=this.$mp.query.index
        let oldStorage =wx.getStorageSync('isCollected')
        if(!oldStorage){
          wx.setStorage({
            key:'isCollected',
            data:{}
          })
        }else{
          // oldStorage[this.strategy.sid]
          this.isCollected=(oldStorage[this.strategy.sid]?true:false)
        }
        },
        mounted(){
            this.detailObj = this.listTmp[this.strategy.sid]
        },
        computed: {
      ...mapState(['listTmp'])
    },

     methods:{
       //点击进入详情

       async handleCollection(){
        let isCollected = !this.isCollected
        this.isCollected =isCollected
        let title=isCollected?'收藏成功':'取消收藏'
        wx.showToast({
          title,
          icon: 'success',
          
        })
           let oldStorage =wx.getStorageSync('isCollected')
          //  oldStorage ={};
          // console.log(this.strategy.sid)
           oldStorage[this.strategy.sid]=isCollected
          console.log(this.strategy.sid,isCollected)//打印
          console.log('后台返回的信息',this.strategy)//打印
           wx.setStorage({
             key:'isCollected',
             data:oldStorage
           })
           if(isCollected==true){
              console.log('收藏')//打印
              // console.log('收藏879',this.strategy.openid)//打印
/////////////收藏
              const data={
        sid:this.strategy.sid,
        // join字符串的形式
        openid:wx.getStorageSync('userinfo').openId,
        fopenid:this.strategy.openid,
        sbiao:this.strategy.sbiao,
        ssrc:this.strategy.ssrc,
        szheng:this.strategy.szheng,
        wx_name:this.strategy.wx_name,
        create_time:this.strategy.create_time,
        avatarurl:this.strategy.avatarurl,
        nssrc:this.strategy.nssrc,
        // wechat:this.wechat,
        
      }
      // console.log('收藏123',this.strategy.create_time)//打印
    try{
       const res = await post('/weapp/addcollectlist',data)
       console.log("从后端返回的执行正确的信息是:",res)
      //  console.log("从后端返回的执行正确的信息是:",data)
       
      
    }catch(e){
      console.log("从后端返回的执行错误的信息是",e)
       showModel('提交失败',"服务器出错")
    }
           }else{
///////////////////////// 未收藏            
              console.log('未收藏')//打印
              const data={
        sid:this.strategy.sid,
        // join字符串的形式
        // src:this.src.join(","),
        // wechat:this.wechat,
        // openid:wx.getStorageSync('userinfo').openId,
        
      }
      // console.log('收藏123',data)//打印
    try{
       const res = await post('/weapp/deletecollectlist',data)
       console.log("从后端返回的执行正确的信息是:",res)
      //  console.log("从后端返回的执行正确的信息是:",data)
       
      
    }catch(e){
      console.log("从后端返回的执行错误的信息是",e)
       showModel('提交失败',"服务器出错")
    }
           }
      },
   

        previewImg(sitem) {
          wx.navigateTo({
           
        url: "../xiangqing/main?sitem="+JSON.stringify(sitem),
        
      }),
       console.log("提交成功图片id",this.strategy.sid)
      //  console.log("提交成功图片",this.strategy.create_time)
      
        },
    
       //收藏
        strategylog(){
          

       console.log("提交成功收藏")
     
       
        },
        mounted(){
      const userinfo =wx.getStorageSync('userinfo')
      if(userinfo.openId){
          this.userinfo = userinfo
          console.log("this.userinfo",this.userinfo)
      }
  }
      //   strategylog_no(){
      //     console.log(this.strategy.sid)

      //  console.log("提交失败收藏")
     
       
      //   }
     }
    
}
</script>

<style lang='scss'>
.page{
  width:100%;
}

//整个框
.i-all{
  position: relative;
  height:250px;
  width:159px;
  float: left;
  margin-top: 20px;
  margin-left:20px;
   box-shadow: 0 5px 5px 2px silver;
    border-radius: 4px;
  // border:1px solid red;
}
//图片
.iimg{
  height:250rpx;
  width:100%;
//  border:1px solid blue;
 border-top-right-radius: 4px;
  border-top-left-radius: 4px;
}

//标题
.isbiao{
  //上右下左
   font-size: 14px;
   padding: 5px 8px 5px 8px;
    margin-left: 10px;
    font-family:"宋体";
    font-weight:bold;
}



//收藏按钮样式
.anniu{
  position:absolute ;
  height:60rpx;
  width:60rpx;
  bottom: 25rpx;
  margin-left: 120px;
  // background:url(https://my-wechatl.oss-cn-beijing.aliyuncs.com/dianzan_1.png);
   background-size: cover;
 
}
</style>
